package com.stray.crawl.worker.task;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.net.URL;
import java.util.UUID;

import com.stray.crawl.Constants.ConfigKey;
import com.stray.crawl.Constants.DefaultSetting;
import com.stray.crawl.BeanContext;
import com.stray.crawl.TaskContext;
import com.stray.crawl.supt.EventPublisher;
import com.stray.crawl.supt.FileStorage;
import com.stray.crawl.utils.IOUtils;
import com.stray.crawl.worker.HttpEnums;
import com.stray.crawl.worker.TaskStatus;

public class DocumentCrawlTask extends CrawlTask {

	private FileStorage storage = BeanContext.get(FileStorage.class);

	public DocumentCrawlTask(EventPublisher<CrawlTask, TaskStatus> publisher, TaskContext taskContext) {
		super(publisher, taskContext);
	}

	@Override
	protected void doAction() throws IOException {
		URL url = taskContext.getUrl();
		String filename = HttpEnums.getFilename(url);
		filename = (filename == null || filename.length() == 0) ? UUID.randomUUID().toString() : filename;

		String path = storage.getResourcePath(filename, taskContext.getConfig(ConfigKey.STORAGE_PREFIX));
		String data = (String) resultContext.getContent();

		Reader in = null;
		try {
			in = new StringReader(data);
			storage.storeToPath(path, in, DefaultSetting.CHARSET);
		} finally {
			IOUtils.closeQuietly(in);
		}
	}

}